#include "execsql.h"
#include "teacherinfo.h"
#include <QDebug>
#include <QSqlQuery>
#include <QSqlRecord>


ExecSQL::ExecSQL()
{}

ExecSQL::~ExecSQL()
{}

AdminInfo ExecSQL::selectAdminInfo(QString id)
{
    AdminInfo info;
    QSqlQuery query;
    QString queryString =
            QString("select * from AdmintorInfoTable where"
                    " ID = '%1'").arg(id);
    qDebug() << queryString;
        if(query.exec(queryString))
        {
              int id_id = query.record().indexOf("ID");
              int pswd_id = query.record().indexOf("loginpw");
              int name_id = query.record().indexOf("name");
              int sex_id = query.record().indexOf("sex");
              int birth_id = query.record().indexOf("birth");
              while(query.next())
              {
                  QString id = query.value(id_id).toString();
                  QString pswd = query.value(pswd_id).toString();
                  QString name= query.value(name_id).toString();
                  QString sex= query.value(sex_id).toString();
                  QString birth= query.value(birth_id).toString();

                  info.updateAdminInfo(id, name,sex,birth,pswd);
              }
    }else{
              qDebug()<<"---------999---------";
    }
    return info;
}



StudentInfo ExecSQL::selectStudentInfo(QString id)
{
    StudentInfo info;
    QSqlQuery query;
    QString queryString =
            QString("select * from StudentInfoTable where "
                    "ID = '%1'").arg(id);
   qDebug() << queryString;
    if(query.exec(queryString))
    {
          int id_id = query.record().indexOf("ID");
          int name_id = query.record().indexOf("name");
          int xuehao_id = query.record().indexOf("xuehao");
          int pswd_id = query.record().indexOf("loginpw");
          int sex_id = query.record().indexOf("sex");
          int classid_id = query.record().indexOf("classid");
          int RxTime_id = query.record().indexOf("RxTime");
          while(query.next())
          {
              QString id = query.value(id_id).toString();
              QString name = query.value(name_id).toString();
              QString xuehao = query.value(xuehao_id).toString();
              QString pswd = query.value(pswd_id).toString();
              QString classid = query.value(classid_id).toString();
              QString sex = query.value(sex_id).toString();
              QString RxTime = query.value(RxTime_id).toString();

              info.updateStudentInfo(id,name,xuehao,pswd,sex,classid,RxTime);
          }
    }else{
        qDebug()<<"---------999---------";
    }
    return info;
}

TeacherInfo ExecSQL::selectTeacherInfo(QString id)
{
    TeacherInfo info;
    QSqlQuery query;
    QString queryString =
            QString("select * from TeacherInfoTable where "
                    "ID = '%1'").arg(id);
     qDebug() << queryString;
    if(query.exec(queryString))
    {
        int id_id = query.record().indexOf("ID");
        int TeacherId_id = query.record().indexOf("TeacherID");
        int XID_id = query.record().indexOf("XID");
        int Name_id = query.record().indexOf("Name");
        int Sex_id = query.record().indexOf("Sex");
        int Birth_id = query.record().indexOf("Birth");
        int pswd_id = query.record().indexOf("loginpw");
        while(query.next())
        {
            QString id = query.value(id_id).toString();
            QString Name = query.value(Name_id).toString();
            QString XID = query.value(XID_id).toString();
            QString pswd = query.value(pswd_id).toString();
            QString TeacherID = query.value(TeacherId_id).toString();
            QString Sex = query.value(Sex_id).toString();
            QString Birth = query.value(Birth_id).toString();

             info.updateTeacherInfo(id, TeacherID,XID, Name,Sex, Birth, pswd);
          }
    }else{
        qDebug()<<"---------999---------";
    }
    return info;
}

CheckinInfo ExecSQL::selectCheckinInfo(QString classid)
{
    CheckinInfo info;
    QSqlQuery query;
    QString queryString =
            QString("select * from CheckinInfoTable where "
                    "Classid = '%1'").arg(classid);
     qDebug() << queryString;
    if(query.exec(queryString))
    {
        int Classid_id = query.record().indexOf("Classid");
        int TeacherId_id = query.record().indexOf("TeacherId");
        int XID_id = query.record().indexOf("XID");
        int xuehao_id = query.record().indexOf("xuehao");
        int RoomID_id = query.record().indexOf("RoomID");
        int CheckinTime_id = query.record().indexOf("CheckinTime");
        int ScienceID_id = query.record().indexOf("ScienceID");
        while(query.next())
        {
            QString Classid = query.value(Classid_id).toString();
            QString xuehao = query.value(xuehao_id).toString();
            QString XID = query.value(XID_id).toString();
            QString ScienceID = query.value(ScienceID_id).toString();
            QString TeacherID = query.value(TeacherId_id).toString();
            QString RoomID = query.value(RoomID_id).toString();
            QString CheckinTime = query.value(CheckinTime_id).toString();

             info.updateCheckinInfo(XID, ScienceID,TeacherID, Classid,RoomID,
                                    CheckinTime, xuehao);
          }
    }else{
        qDebug()<<"---------999---------";
    }
    return info;
}
AdminInfo ExecSQL::insertAdminInfo(const QString &id,
                                   const QString &name ,const QString &sex,
                                              const QString &birth, const QString &pswd)
{
    AdminInfo info;
    QSqlQuery query;
    QString queryString = QString("INSERT INTO AdmintorInfoTable (ID, Name, Sex, Birth, loginpw)  VALUES ('"
                            +id+"', '"+name+ "', '"+sex+"', '"+
                            birth+"', '"+pswd+"');");
    qDebug() << queryString;
        if(query.exec(queryString))
        {
              int id_id = query.record().indexOf("ID");
              int pswd_id = query.record().indexOf("loginpw");
              int name_id = query.record().indexOf("name");
              int sex_id = query.record().indexOf("sex");
              int birth_id = query.record().indexOf("birth");
              while(query.next())
              {
                  QString id = query.value(id_id).toString();
                  QString pswd = query.value(pswd_id).toString();
                  QString name= query.value(name_id).toString();
                  QString sex= query.value(sex_id).toString();
                  QString birth= query.value(birth_id).toString();

                  info.updateAdminInfo(id, name,sex,birth,pswd);
              }
    }else{
              qDebug()<<"---------999---------";
    }
    return info;
}
StudentInfo ExecSQL::insertStudentInfo(const QString &id,const QString &name,
                                       const QString &xuehao,const QString &pswd,
                                       const QString &sex,const QString &classid,const QString &RxTime)
{
    StudentInfo info;
    QSqlQuery query;
    QString queryString =
            QString("INSERT INTO StudentInfoTable (ID, xuehao, Name, loginpw, Sex,Classid,RxTime)  VALUES ('%1','%2','%3','%4','%5','%6','%7'"
                    ");").arg(id).arg(name).arg(xuehao).arg(pswd).arg(sex).arg(classid).arg(RxTime);
   qDebug() << queryString;
    if(query.exec(queryString))
    {
          int id_id = query.record().indexOf("ID");
          int name_id = query.record().indexOf("name");
          int xuehao_id = query.record().indexOf("xuehao");
          int pswd_id = query.record().indexOf("loginpw");
          int sex_id = query.record().indexOf("sex");
          int classid_id = query.record().indexOf("classid");
          int RxTime_id = query.record().indexOf("RxTime");
          while(query.next())
          {
              QString id = query.value(id_id).toString();
              QString name = query.value(name_id).toString();
              QString xuehao = query.value(xuehao_id).toString();
              QString pswd = query.value(pswd_id).toString();
              QString classid = query.value(classid_id).toString();
              QString sex = query.value(sex_id).toString();
              QString RxTime = query.value(RxTime_id).toString();

              info.updateStudentInfo(id,name,xuehao,pswd,sex,classid,RxTime);
          }
    }else{
        qDebug()<<"---------999---------";
    }
    return info;
}

TeacherInfo ExecSQL::insertTeacherInfo(const QString &id, const QString &teacherID,
                                       const QString &XID, const QString &Name,const QString
                                                &Sex, const QString &Birth, const QString &pswd)
{
    TeacherInfo info;
    QSqlQuery query;
    QString queryString =
            QString("INSERT INTO TeacherInfoTable (ID, TeacherID, XID, Name, Sex,Birth,loginpw)  VALUES ('%1','%2','%3','%4','%5','%6','%7'"
                    ");").arg(id).arg(teacherID).arg(XID).arg(Name).arg(Sex).arg(Birth).arg(pswd);
     qDebug() << queryString;
    if(query.exec(queryString))
    {
        int id_id = query.record().indexOf("ID");
        int TeacherId_id = query.record().indexOf("TeacherID");
        int XID_id = query.record().indexOf("XID");
        int Name_id = query.record().indexOf("Name");
        int Sex_id = query.record().indexOf("Sex");
        int Birth_id = query.record().indexOf("Birth");
        int pswd_id = query.record().indexOf("loginpw");
        while(query.next())
        {
            QString id = query.value(id_id).toString();
            QString Name = query.value(Name_id).toString();
            QString XID = query.value(XID_id).toString();
            QString pswd = query.value(pswd_id).toString();
            QString TeacherID = query.value(TeacherId_id).toString();
            QString Sex = query.value(Sex_id).toString();
            QString Birth = query.value(Birth_id).toString();

             info.updateTeacherInfo(id, TeacherID,XID, Name,Sex, Birth, pswd);
          }
    }else{
        qDebug()<<"---------999---------";
    }
    return info;
}
CheckinInfo ExecSQL::insertCheckinInfo(const QString &XID,const QString &ScienceID,
                                     const QString &teacherId,const QString &Classid,
                                     const QString &RoomID,const QString &CheckinTime,
                                     const QString &xuehao)
{
    CheckinInfo info;
    QSqlQuery query;
    QString queryString =
            QString("INSERT INTO CheckinInfoTable (XID, ScienceID, teacherId, Classid, RoomID,"
                    "CheckinTime,xuehao)  VALUES ('%1','%2','%3','%4','%5','%6','%7'"
                    ");").arg(XID).arg(ScienceID).arg(teacherId).arg(Classid).arg(RoomID).
            arg(CheckinTime).arg(xuehao);
     qDebug() << queryString;
    if(query.exec(queryString))
    {
        int Classid_id = query.record().indexOf("Classid");
        int TeacherId_id = query.record().indexOf("TeacherId");
        int XID_id = query.record().indexOf("XID");
        int xuehao_id = query.record().indexOf("xuehao");
        int RoomID_id = query.record().indexOf("RoomID");
        int CheckinTime_id = query.record().indexOf("CheckinTime");
        int ScienceID_id = query.record().indexOf("ScienceID");
        while(query.next())
        {
            QString Classid = query.value(Classid_id).toString();
            QString xuehao = query.value(xuehao_id).toString();
            QString XID = query.value(XID_id).toString();
            QString ScienceID = query.value(ScienceID_id).toString();
            QString TeacherID = query.value(TeacherId_id).toString();
            QString RoomID = query.value(RoomID_id).toString();
            QString CheckinTime = query.value(CheckinTime_id).toString();

            info.updateCheckinInfo(XID, ScienceID,TeacherID, Classid,RoomID,
                                    CheckinTime, xuehao);
          }
    }else{
        qDebug()<<"---------999---------";
    }
    return info;
}
